/* 
 * Copyright (C) 2007  Brunata a/s
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 */

package com.brunata.httprmi;

import java.lang.reflect.Proxy;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Utility/factory class for the construction of a dynamic proxy adhering to a
 * gived contract as passed to the create method.
 * 
 * @author Casper Bang (CAB@brunata.com)
 */
public final class DynamicProxyFactory
{
    ////////////////////////////////////////////////////////////////////////////
    // Constants
    ////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////
    // Constructors
    ////////////////////////////////////////////////////////////////////////////
    
    /**
     * Private constructor to disallow instantiation
     */ 
    private DynamicProxyFactory()
    {
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internals
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////
    // Overrides/implementations
    ////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////
    // Accessors (getters/setters)
    //////////////////////////////////////////////////////////////////////////// 

    /**
     * Factory metnod for creating a dynamic proxy, adhering to whichever contract is passed as the first argument
     * 
     * @param <B>Class</B> the <B>Interface</B> which we mimic with this proxy
     * @param <B>String</B> the enpoint where a matching HttpRmiServlet subclass is located
     * @param <B>ProgressListener</B> implementation, which will receive progress notifications
     * @returns The dynamic proxy, ready to be used
     */ 
    @SuppressWarnings("unchecked")
    public static final <T> T create(Class<T> c, String urlString, IStakeHolder listener)
    {
        return (T)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {c, ICancellable.class}, new HttpRmiProxy(urlString, listener)) ;
    }
    
    /**
     * Factory metnod for creating a dynamic proxy, adhering to whichever contract is passed as the first argument
     * 
     * @param <B>Class</B> the <B>Interface</B> which we mimic with this proxy
     * @param <B>String</B> the enpoint where a matching HttpRmiServlet subclass is located
     * @returns The dynamic proxy, ready to be used
     */ 
    @SuppressWarnings("unchecked")
    public static final <T> T create(Class<T> c, String urlString )
    {
        return DynamicProxyFactory.create(c, urlString, new IStakeHolder()
                {
                    public void progressChangeEvent(int level, int min, int max)
                    {
                         Logger.getLogger("global").log(Level.FINEST, level +" bytes out of " + max + " received...");
                    }

                    public boolean isCancelled()
                    {
                        return false;
                    }
                }
        
        
        
        );
    }
}
